#ifndef FELHASZNALO_H
#define FELHASZNALO_H

class Felhasznalo
{
        std::string _name;
        std::string _password;
        std::string _email;
        std::string _address;
        std::string _phone;
    public:
        Felhasznalo(){}
        Felhasznalo(std::string name, std::string password, std::string email, std::string address, std::string phone):
            _name(name),_password(password),_email(email),_address(address),_phone(phone){}
        Felhasznalo(Felhasznalo& f)
        {
            _name = f._name;
            _password = f._password;
            _email = f._email;
            _address = f._address;
            _phone = f._phone;
        }

        Felhasznalo(const Felhasznalo& f)
        {
            _name = f._name;
            _password = f._password;
            _email = f._email;
            _address = f._address;
            _phone = f._phone;
        }
        std::string GetName()
        {
            return _name;
        }
        std::string GetEmail()
        {
            return _email;
        }
        std::string GetAddress()
        {
            return _address;
        }
        std::string GetPhone()
        {
            return _phone;
        }
        bool TryPassword(std::string password)
        {
            return _password == password;
        }
        friend std::ostream& operator<<(std::ostream& os, const Felhasznalo& f)
        {
            os << f._name << ";" << f._password << ";" << f._email << ";" << f._address << ";" << f._phone << ";" << std::endl;
            return os;
        }
};

class Bejelentkezes
{
        Lista<Felhasznalo*> _felhasznalok;
        Felhasznalo* _f;
    public:
        Bejelentkezes()
        {
            std::cout << "======================================" << std::endl;
            std::cout << "========UDVOZLUNK KOCKAFOLDEN!========" << std::endl;
            std::cout << "======================================" << std::endl ;
            LoadFelhasznaloData();
            _f = NULL;
            std::string v, fn, pw;
            v = "";
            fn = "";
            pw = "";
            while (!_f)
            {
                while (v != "b" && v!= "r")
                {
                    std::cout << std::endl << "Valasszon bejelentkezest vagy regisztraciot (b/r): ";
                    std::cin >> v;
                }
                if (v == "b")
                {
                    std::cout << std::endl << "=== Bejelentkezes === " << std::endl;
                    while (fn == "")
                    {
                        std::cout << "- Felhasznalonev: ";
                        std::cin >> fn;
                    }
                    while (pw == "")
                    {
                        std::cout << "- Jelszo: ";
                        std::cin >> pw;
                    }
                    _f = Login(fn, pw);
                    if (!_f)
                    {
                        std::cout << "Ilyen felhasznalonev jelszo paros nem letezik!" << std::endl;
                    }
                }
                else if (v == "r")
                {
                    std::string ad, em, ph;
                    ad = "";
                    em = "";
                    ph = "";
                    std::cout << "=== Regisztracio === " << std::endl;
                    while (fn == "")
                    {
                        std::cout << "- Felhasznalonev: ";
                        std::cin >> fn;
                        Iterator<Felhasznalo*> it =  _felhasznalok.Begin();
                        while (it != _felhasznalok.End() && (*it)->GetName() != fn)
                        {
                            ++it;
                        }
                        if (it != _felhasznalok.End())
                        {
                            std::cout << "Ilyen felhasznalonevvel mar letezik felhasznalo!" << std::endl;
                            fn = "";
                        }
                    }
                    while (pw == "")
                    {
                        std::cout << "- Jelszo: ";
                        std::cin >> pw;
                    }
                    while (em == "")
                    {
                        std::cout << "- Email: ";
                        std::cin >> em;
                    }
                    while (ad == "")
                    {
                        std::cout << "- Cim: ";
                        std::cin >> ad;
                    }
                    while (ph == "")
                    {
                        std::cout << "- Telefonszam: ";
                        std::cin >> ph;
                    }
                    _f = new Felhasznalo(fn, pw, em, ad, ph);
                    _felhasznalok.InsertLast(_f);
                }
                v = "";
                fn = "";
                pw = "";
            }
        }
        Felhasznalo* CurrentFelhasznalo()
        {
            return _f;
        }
        Lista<Felhasznalo*>& Felhasznalok()
        {
            return _felhasznalok;
        }
        Felhasznalo* Login(std::string name, std::string password)
        {
            Iterator<Felhasznalo*> it =  _felhasznalok.Begin();
            while (it != _felhasznalok.End() && (*it)->GetName() != name)
            {
                ++it;
            }
            if (it != _felhasznalok.End() && (*it)->TryPassword(password))
            {
                return (*it);
            }
            return NULL;
        }
        void LoadFelhasznaloData()
        {
            std::ifstream f("felhasznalok.dat");
            if (f.fail())
            {
                std::cout << "Hianyzik a felhasznalok.dat!" << std::endl;
                std::exit(1);
            }
            std::string tmp;
            while(std::getline(f, tmp))
            {
                std::string fn, pw, em, ad, ph;
                size_t pos = 0;
                std::string token;
                int i = 0;
                while ((pos = tmp.find(";")) != std::string::npos) {
                    token = tmp.substr(0, pos);
                    switch (i)
                    {
                        case 0 : fn = token; break;
                        case 1 : pw = token; break;
                        case 2 : em = token; break;
                        case 3 : ad = token; break;
                        case 4 : ph = token; break;
                    }
                    tmp.erase(0, pos + 1);
                    ++i;
                }
                _felhasznalok.InsertLast(new Felhasznalo(fn, pw, em, ad, ph));
            }
            f.close();
        }
        void SaveFelhasznaloData()
        {
            std::ofstream f("felhasznalok.dat");
            Iterator<Felhasznalo*> it =  _felhasznalok.Begin();
            while (it != _felhasznalok.End())
            {
                f << *(*it);
                ++it;
            }
            f.close();
        }
};

#endif
